package logic
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	import logic.state.Hall;
	import logic.state.Loading;
	import logic.state.Play;
	import logic.state.StateBase;
	import net.Net;
	
	/**
	 * @file	Game.as
	 * @author 	HOTLALA
	 * @date	2013/Apr 16, 2013/10:36:52 PM
	 */
	public class Game extends Sprite
	{
		private var _ping_timer:Timer = new Timer(1000);
		private var _tick:Timer = new Timer(1000);
		private var _state:StateBase = null;
		private var _res_path:String = "http://news.k8008.com/UpLoad/JPG/2007/9/12/3HS1566GG8Z5OW75.JPG";
		private var _states:Dictionary = new Dictionary;
		private var _loader:Loader = new Loader;
		
		public function Game()
		{
			_states[Global.Loading] = Loading;
			_states[Global.Hall] = Hall;
			_states[Global.Play] = Play;
			
			//_ping_timer.addEventListener(TimerEvent.TIMER,_on_ping);
			//_ping_timer.start();
			_loader.load(new URLRequest(_res_path));
			_loader.addEventListener(Event.COMPLETE, _start);
			Net.instance().init(function():void {
				_ping_timer.addEventListener(TimerEvent.TIMER,_on_ping);
				_ping_timer.start();
			});
		}
		
		private function _start():void 
		{
			_tick.addEventListener(TimerEvent.TIMER, _on_tick);
			_state = new _states[Global.Hall](this);
			_state.begin();
		}
		public function _chg_state(_new:int):void 
		{
			if(_state)
				_state.end();
			_state = new _states[_new](this)
			if(_state)
				_state.begin();
		}
		
		private function _on_tick(e:TimerEvent):void 
		{
			if(_state)
				_state.tick();
		}
		
		private function _on_frame(evt:Event):void
		{
				
		}
		
		private function _on_ping(evt:Event):void
		{
			//Net.instance().ping();
		}
		
		
	}
}